package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.common.LineUtil;
import com.ruoyi.syrw.common.QuadFunction;
import com.ruoyi.report_tg.domain.vo.GeneralCharDataVo;
import com.ruoyi.report_wjjhl.domain.WdclzdgmdBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdclzdgmdBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.WdclzdgmdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdcljsjcBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdcljsjcBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdcljsjcBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdcljsjcBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdcljsjcBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料击实检测检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdcljsjcBsInfoServiceImpl implements IWdcljsjcBsInfoService {

    private final WdcljsjcBsInfoMapper baseMapper;
    private final WdclzdgmdBaDetailMapper detailMapper;

    /**
     * 查询无机结合料稳定材料击实检测检测记录
     */
    @Override
    @FIllBasicInfo
    public WdcljsjcBsInfoVo queryById(Long excelId){
        WdcljsjcBsInfoVo wdcljsjcBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdclzdgmdBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdclzdgmdBaDetail>()
            .eq(WdclzdgmdBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdcljsjcBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return compute(wdcljsjcBsInfoVo);
    }

    /**
     * 查询无机结合料稳定材料击实检测检测记录列表
     */
    @Override
    public TableDataInfo<WdcljsjcBsInfoVo> queryPageList(WdcljsjcBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdcljsjcBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdcljsjcBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料击实检测检测记录列表
     */
    @Override
    public List<WdcljsjcBsInfoVo> queryList(WdcljsjcBsInfoBo bo) {
        LambdaQueryWrapper<WdcljsjcBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdcljsjcBsInfo> buildQueryWrapper(WdcljsjcBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdcljsjcBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdcljsjcBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdcljsjcBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), WdcljsjcBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdcljsjcBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdcljsjcBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdcljsjcBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdcljsjcBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdcljsjcBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdcljsjcBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdcljsjcBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdcljsjcBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), WdcljsjcBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdcljsjcBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdcljsjcBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdcljsjcBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdcljsjcBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdcljsjcBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdcljsjcBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdcljsjcBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdcljsjcBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdcljsjcBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancff()), WdcljsjcBsInfo::getJiancff, bo.getJiancff());
        lqw.eq(bo.getMeicjs() != null, WdcljsjcBsInfo::getMeicjs, bo.getMeicjs());
        lqw.eq(bo.getJiehlzl() != null, WdcljsjcBsInfo::getJiehlzl, bo.getJiehlzl());
        lqw.eq(bo.getChanpbl() != null, WdcljsjcBsInfo::getChanpbl, bo.getChanpbl());
        lqw.eq(bo.getShiyzdlj() != null, WdcljsjcBsInfo::getShiyzdlj, bo.getShiyzdlj());
        lqw.eq(bo.getTongrj() != null, WdcljsjcBsInfo::getTongrj, bo.getTongrj());
        lqw.eq(bo.getJiczl() != null, WdcljsjcBsInfo::getJiczl, bo.getJiczl());
        lqw.eq(bo.getLuoj() != null, WdcljsjcBsInfo::getLuoj, bo.getLuoj());
        lqw.eq(bo.getJiehljl() != null, WdcljsjcBsInfo::getJiehljl, bo.getJiehljl());
        lqw.eq(bo.getZuidgmd() != null, WdcljsjcBsInfo::getZuidgmd, bo.getZuidgmd());
        lqw.eq(bo.getZuijhsl() != null, WdcljsjcBsInfo::getZuijhsl, bo.getZuijhsl());
        lqw.eq(bo.getChaoccklhl() != null, WdcljsjcBsInfo::getChaoccklhl, bo.getChaoccklhl());
        lqw.eq(bo.getJiaozhzdgmd() != null, WdcljsjcBsInfo::getJiaozhzdgmd, bo.getJiaozhzdgmd());
        lqw.eq(bo.getJiaozhzjhsl() != null, WdcljsjcBsInfo::getJiaozhzjhsl, bo.getJiaozhzjhsl());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料击实检测检测记录
     */
    @Override
    public Boolean insertByBo(WdcljsjcBsInfoBo bo) {
        WdcljsjcBsInfo add = BeanUtil.toBean(bo, WdcljsjcBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 5次
            ArrayList<WdclzdgmdBaDetail> details = new ArrayList<>(5);
            for (long i = 1; i <= 5L; i++) {
                details.add(new WdclzdgmdBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料击实检测检测记录
     */
    @Override
    @DealHeh
    public Boolean updateByBo(WdcljsjcBsInfoBo bo) {
        WdcljsjcBsInfo update = BeanUtil.toBean(bo, WdcljsjcBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdclzdgmdBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdcljsjcBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料击实检测检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public WdcljsjcBsInfoVo compute(WdcljsjcBsInfoVo vo) {
        // 1. 参数判断 如果有任何一个为空,则 直接原样返回
        List<WdclzdgmdBaDetailVo> baDetailVos = vo.getBaDetailVos();
        if (CollUtil.isEmpty(baDetailVos)) {
            return vo;
        }
        for (WdclzdgmdBaDetailVo detailVo : baDetailVos) {
            if (BigDecimalUtil.isAnyOneEmpty(detailVo.getGanmd(), detailVo.getHanslpjz())) {
                return vo;
            }
        }
        // 2. 计算
        double[][] data = new double[5][2];
        for (int i = 0; i < baDetailVos.size(); i++) {
            data[i][0] = baDetailVos.get(i).getHanslpjz().doubleValue();
            data[i][1] = baDetailVos.get(i).getGanmd().doubleValue();
        }
        QuadFunction quadFuncLine = LineUtil.getQuadFuncLine(data);
        vo.setZuijhsl(BigDecimalUtil.defaultValue((quadFuncLine.getB() / ((0 - quadFuncLine.getA()) * 2)), 2));
        vo.setZuidgmd(BigDecimalUtil.defaultValue(quadFuncLine.getY(vo.getZuijhsl().doubleValue()), 2));
        // 3.1 画图所需要的 data  参与画线 不展示 不突出辅助线
        List<GeneralCharDataVo> charData = generateData(quadFuncLine, baDetailVos.get(0).getHanslpjz().doubleValue(),
            baDetailVos.get(4).getHanslpjz().doubleValue());
        // 3.2 其他的点 不参与画线 展示 不突出辅助线
        for (double[] datum : data) {
            charData.add(new GeneralCharDataVo(datum, "010"));
        }
        // 3.3 参与画线 展示 突出辅助线
        charData.add(new GeneralCharDataVo(new double[]{vo.getZuijhsl().doubleValue(), vo.getZuidgmd().doubleValue()}, "011"));
        vo.setChartData(charData);
        return vo;
    }

    private List<GeneralCharDataVo> generateData(QuadFunction quadFuncLine, double start, double end) {
        List<GeneralCharDataVo> result = new ArrayList<>(600);
        for (double i = start; i <= end; i += 0.2) {
            result.add(new GeneralCharDataVo(new double[]{BigDecimalUtil.defaultValue(i,2).doubleValue(), quadFuncLine.getY(i)}, "100"));
        }
        return result;

    }
}
